<?php
namespace app\api\controller\disp;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\buss\Buss as BussModel;
use app\common\model\orderdelivery\Disposalsite as Disp;
use app\api\controller\Base;
use app\common\model\power\Rolepower;
//财务信息
class Disposalsite extends Base
{
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(203010);
	}

	/**
	 * 获取企业信息
	 * @return [type] [description]
	 */
	public function getinfo(){
		$id = input('id');
		$company = Disp::user_getinfo($id);
		if(!empty($company)){
			return myok($company);
		}else{
			return myerr('没有找到此处置单位');
		}
	}

	/**
	 * 产废单位列表
	 * @return [type] [description]
	 */
	public function list(){
		$data = input('get.');
		$data['Type'] = 'disposalsite';
		$data = init_where($data);
		$list = Disp::usergetlist($data,'ware');
		foreach ($list['list'] as $key => $value) {
			if(empty($value['bussinfo']['UserName'])){
				$list['list'][$key]['Name'] = '暂无';
			}else{
				$list['list'][$key]['Name'] = $value['bussinfo']['UserName'];
			}
		}
		$list['tr']['title']  = ['处置单位','登记时间','合作状态','查看'];
		$list['tr']['key'] = ['DisposalSiteName','CreateTime','State','DisposalSiteId'];
		return myok($list);
	}
	/**
	 * 修改/完善企业信息 
	 * 产废人信息也再次完善
	 * @return [type] [description]
	 */
	public function reinfo(){
		$data = input('post.');
		//为空则给企业返回修改信息
		if(empty($data['id'])){
			$disp = Disp::user_create($data);
			return myok('处置单位创建成功');
		}else{
			//修改企业信息
			$sb = Disp::user_reinfo($data);
			return myok('处置单位修改成功');
		}
	}

	/**
	 * 图片上传
	 * @return [type] [description]
	 */
	public function upimg(){
		$type = empty(input('type'))?'other':input('type');
		if(!$type=='company'||!$type=='user'||!$type=='order'){
			$type = 'other';
		}
		try {
			$file = request()->file('img');
		} catch (\Exception $e) {
			return json_encode(['status'=>'error','data'=>$e->getMessage()]);
		}
        // 移动到框架应用根目录/public/uploads/ 目录下
        if(!is_null($file)){
           	try{
           		$save_path = Env::get('root_path').'public\\static' .'\\upimg\\buss\\'.$type.'\\';
	            $info = $file->validate(["size"=>50000000,'ext'=>'jpg,png,gif'])->rule("date")->move($save_path);
	            //echo $save_path. DS . date("Ymd") . DS .  $info->getFilename();
	            $image = \think\Image::open($save_path . '\\' . $info->getSaveName());  
	            $image->thumb(2000, 2000)->save($save_path .  '\\' . $info->getSaveName());  
	            if($info){//getSaveName();
	                $p=$info->getFilename();
	                return json_encode(['status'=>'ok','data'=>SERVERURL.'static/upimg/buss/'.$type.'/'.date('Ymd',time()).'/'.$p]);
	            }else{
	              return json_encode(['status'=>'error','data'=>$info->getError()]);
	            }
           	} catch (\Exception $e) {
				return json_encode(['status'=>'error','data'=>$e->getMessage()]);
			}	
            
         }else{
              return json_encode(['status'=>'error','data'=>'没有找到需要上传的图片']);
         }
	}


	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
}
